package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanShopQO;
import com.xingkeduo.pojo.table.CraftsmanShopTO;
import com.xingkeduo.pojo.table.CraftsmanTO;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * 求职者封装类
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:41:24
 * @description
 */
@Component
public class CraftsmanShopService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanShopTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanShopTO, CraftsmanShopQO> dao;
    private final static String errorMsg = "一线员工召回信息";

    /**
     * 查询最新一条
     *
     * @param craftsmanId
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月14日 下午1:53:08
     */
    public List<CraftsmanShopTO> selectLastByCraftsmanId(long craftsmanId) throws Throwable {
        CraftsmanShopQO query = new CraftsmanShopQO().setCraftsmanId(craftsmanId);
        return selectByQuery(query);
    }


    public CraftsmanShopTO selectByPrimary(long id) throws Throwable {
        return dao.select(NAMESPACE, id);
    }

    /**
     * 查询一条
     *
     * @param craftsmanId
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月14日 下午1:53:08
     */
    public CraftsmanShopTO selectByCraftsmanIdAndShopId(long craftsmanId, long shopId) throws Throwable {
        CraftsmanShopQO query = new CraftsmanShopQO().setCraftsmanId(craftsmanId).setShopId(shopId);
        List<CraftsmanShopTO> shopList = selectByQuery(query);
        if (CommonUtil.isEmpty(shopList))
            return null;
        return shopList.get(0);
    }

    /**
     * 统计
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月15日 下午8:32:13
     */
    public int countByQuery(CraftsmanShopQO query) throws Throwable {
        return dao.selectCount(NAMESPACE, query);
    }

    /**
     * 查询
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月14日 下午1:53:03
     */
    public List<CraftsmanShopTO> selectByQuery(CraftsmanShopQO query) throws Throwable {
        return dao.select(NAMESPACE, query);
    }

    /**
     * 新增
     *
     * @param craftsmanShop
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月14日 下午1:52:58
     */
    public void insert(CraftsmanShopTO craftsmanShop) throws Throwable {
        if (dao.insert(NAMESPACE, craftsmanShop.initId(IdentityUtil.nextId(Worker.USER))) < 1)
            throw new DataException(DataException.Type.FAIL, errorMsg);
    }

    /**
     * 新增
     *
     * @param craftsmanBackList
     * @param craftsman
     * @param operator
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月14日 下午1:52:53
     */
    public void insert(List<CraftsmanShopTO> craftsmanShopList, CraftsmanTO craftsman, long operator) throws Throwable {
        for (Iterator<CraftsmanShopTO> iterator = craftsmanShopList.iterator(); iterator.hasNext(); ) {
            iterator.next().initId(IdentityUtil.nextId(Worker.USER)).setCreatedBy(operator).setUpdatedBy(operator);
        }
        if (dao.insert(NAMESPACE, craftsmanShopList) < craftsmanShopList.size())
            throw new DataException(DataException.Type.FAIL, errorMsg);
    }

    /**
     * 修改
     *
     * @param shopTO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/4/23 11:15
     */
    @Transactional
    public void update(CraftsmanShopTO shopTO) throws Throwable {
        if (dao.update(NAMESPACE, shopTO) < 1)
            throw new DataException(DataException.Type.FAIL, errorMsg);
    }

    /**
     * 批量修改
     *
     * @param shopTO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/4/23 11:15
     */
    @Transactional
    public int updateList(List<CraftsmanShopTO> list) throws Throwable {
        int count = 0;
        for (Iterator<CraftsmanShopTO> iterator = list.iterator(); iterator.hasNext(); ) {
            count += dao.update(NAMESPACE, iterator.next());
        }
        return count;
    }


    @Transactional
    public void insertList(List<CraftsmanShopTO> list) throws Throwable {
        if (dao.insert(NAMESPACE, list) < list.size()) {
            throw new DataException(DataException.Type.FAIL, "文件保存失败");
        }
    }

    /**
     * 删除
     *
     * @param craftsmanShop
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月15日 下午8:25:25
     */
    public void delete(CraftsmanShopTO craftsmanShop) throws Throwable {
        craftsmanShop.setIsDelete(YesNoEnum.YES);
        if (dao.update(NAMESPACE, craftsmanShop) < 1)
            throw new DataException(DataException.Type.FAIL, errorMsg);
    }
}
